<!DOCTYPE html>
<html>
<head>
<style>
body {
	font-size: 12px;
	color: red;
	overflow-x:hidden;
}

a {
	color:#0000CC;
	text-decoration: underline;
	cursor: pointer;
}

td { vertical-align: top }

.item_title {
	display: block;
	min-width: 300px;
	cursor: pointer;
}

.error {
	white-space: nowrap;
	color: red;
}

.more {
	display: block;
	text-align: right;
	padding-top: 20px;
	padding-right: 10px;
	color: #88C;
}
</style>

<script>
function buildPreview() {
	if (chrome.extension.getBackgroundPage().initialized) {
		var doc = chrome.extension.getBackgroundPage().doc;
		var maxFeedItems = localStorage["numberStories"];
		var feed = document.getElementById("feed");
		var entries = doc.getElementsByTagName('entry');
  
		if (entries.length == 0) {
			entries = doc.getElementsByTagName('item');
		}
		var count = Math.min(entries.length, maxFeedItems);
		
		// Parse and format each item
		for (var i = 0; i < count; i++) {
			item = entries.item(i);

			var itemDesc = item.getElementsByTagName('description')[0];
			if (itemDesc) {
				itemDesc = itemDesc.childNodes[0].nodeValue;
			} else {
				itemDesc = '';
			}

			var item = document.createElement("div");
			item.className = "item";
			
			// Insert link break for new column
			if ((itemDesc.indexOf("First column, 1st story") != -1) ||
				(itemDesc.indexOf("Second column, 1st story") != -1) ||
				(itemDesc.indexOf("Third column, 1st story") != -1)) {
				var divider = document.createElement("body");
				divider.innerHTML = "<hr>";
				item.appendChild(divider);
			}
			
			var desc = document.createElement("body");
			desc.scrolling = "no";
			desc.className = "item_title";
			item.appendChild(desc);
			feed.appendChild(item);
			
			// Save link url for later
			var link = itemDesc.substring(itemDesc.indexOf("<a href=") + 9, itemDesc.indexOf(">link</a>") - 1);

			// Remove excessive text and white space
			desc.innerHTML=remove(itemDesc.substring(0,itemDesc.indexOf("(Main")), "<br><br>");
			if (!desc.innerHTML) {
			  desc.innerHTML=remove(itemDesc.substring(0,itemDesc.indexOf("(First")), "<br><br>");
			}
			if (!desc.innerHTML) {
			  desc.innerHTML=remove(itemDesc.substring(0,itemDesc.indexOf("(Second")), "<br><br>");
			}
			if (!desc.innerHTML) {
			  desc.innerHTML=remove(itemDesc.substring(0,itemDesc.indexOf("(Third")), "<br><br>");
			}
			
			// Remove leading line break to reduce white space
			if (desc.innerHTML.indexOf("<br>") == 0) {
				desc.innerHTML = desc.innerHTML.substring(4);
			}
			
			// Add new item indicator
			var found = false;
			for (var z = 0; z < chrome.extension.getBackgroundPage().starItems.length; z++) {
				if (chrome.extension.getBackgroundPage().starItems[z].indexOf(desc.innerHTML.substring(desc.innerHTML.indexOf("<u>"), desc.innerHTML.indexOf("</u>"))) != -1) {
					found = true;
					break;
				}
			}
			if (found) {
				desc.innerHTML = "<table border=0><tr><td><b>!</b></td><td>" + desc.innerHTML;
			} else {
				desc.innerHTML = "<table border=0><tr><td><b> </b></td><td>" + desc.innerHTML;
			}
			
			// Add tooltip
			desc.innerHTML = "<div title=\"" + link + "\">" + desc.innerHTML + "</div>";
			
			desc.addEventListener("click", showLink);
		}
  
		// Finally, add link to Drudge Report
		var more = document.createElement("a");
		more.className = "more";
		more.innerText = "drudgereport.com \u00BB";
		more.addEventListener("click", moreStories);
		feed.appendChild(more);
		
		// Mark all items as read
		chrome.browserAction.setBadgeText({text:""});
		chrome.extension.getBackgroundPage().updateOldItems();
		chrome.extension.getBackgroundPage().starItems = [];
	}
}

function remove(s, t) {
	i = s.indexOf(t);
	r = "";
	if (i == -1) {
		return s;
	}
	r += s.substring(0,i) + remove(s.substring(i + t.length), t);
	return r;
}

function showUrl(url) {
  chrome.tabs.create({url: url});
}

function showLink(event) {
	var item = event.currentTarget.parentNode;
	var tdoc = chrome.extension.getBackgroundPage().req.responseXML;
	var items = tdoc.getElementsByTagName('item');
	var itemDesc;
	
	for (var i = 0; i < items.length; i++) {
		itemDesc = items.item(i).getElementsByTagName('description')[0].childNodes[0].nodeValue;
		itemDesc = itemDesc.substring(0, itemDesc.indexOf("link") + 8);
		if (itemDesc.indexOf(remove(item.textContent, "\n").substring(1)) != -1) {
			showUrl(itemDesc.substring(itemDesc.indexOf("<a href=") + 9, itemDesc.indexOf(">link</a>") - 1));
			break;
		}
	}
}

function moreStories(event) {
  showUrl("http://www.drudgereport.com");
}
</script>
</head>
<body onload="buildPreview();">
<a id="title_a"><img id='title'></a>
<div id="feed"></div>
</body>
</html>